package com.weme.common.util

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import com.weme.common.R

object GlobalLoadingDialog {

    private var loadingDialog: Dialog? = null

    fun showDialog(context: Context) {
        if (loadingDialog?.isShowing == true) {
            return // 避免重复显示
        }

        // 创建 loading 对话框
        loadingDialog = Dialog(context)
        loadingDialog?.setCancelable(false) // 不可取消
        loadingDialog?.setCanceledOnTouchOutside(false) // 点击外部不可取消

        // 设置自定义布局
        val view = LayoutInflater.from(context).inflate(R.layout.progress_dialog_layout, null)
        loadingDialog.apply {
            this!!.window?.setBackgroundDrawableResource(android.R.color.transparent)
        }
        loadingDialog?.setContentView(view)

        // 显示 loading 对话框
        loadingDialog?.show()
    }

    fun dismiss() {
        loadingDialog?.dismiss()
    }
}
